//
//  MineNogiftViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/9.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//我的-我的礼物-送出的礼物

#import "MineNogiftViewController.h"
#import "MineGiftOperator.h"
#import "MineGiftModel.h"
#import "MineGiftDataModel.h"
#import "MineGiftListModel.h"
#import "MJRefresh.h"
#import "MineGiftTableViewCell.h"

@interface MineNogiftViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *giftLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MineNogiftViewController

static NSString *giftCellIdentifier = @"MineGiftTableViewCell";

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (MJRefreshAutoNormalFooter *)footer
{
    if (_footer == nil) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadMore)];
        
    }
    return _footer;
}

- (void)setupView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineGiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:giftCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"MineGiftTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    MineGiftListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Action"] = @"send";
    dic[@"row"] = @"30";
    dic[@"page"] = [NSString stringWithFormat:@"%ld", self.page];
    MineGiftOperator *operator = [[MineGiftOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_header endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = [NSMutableArray arrayWithArray:operator.data.data.list];
            self.giftLabel.text = [NSString stringWithFormat:@"当前送出的礼物数：%ld", operator.data.data.amount];
            if (self.dataArray.count < 30) {
                self.tableView.mj_footer = nil;
            }else{
                self.tableView.mj_footer = self.footer;
                self.page ++;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_header endRefreshing];
    }];

}

- (void)downloadMore
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Action"] = @"send";
    dic[@"row"] = @"30";
    dic[@"page"] = [NSString stringWithFormat:@"%ld", self.page];
    MineGiftOperator *operator = [[MineGiftOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_footer endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataArray];
            [array addObjectsFromArray:operator.data.data.list];
            self.dataArray = array;
            if (operator.data.data.list.count < 30) {
                self.tableView.mj_footer = nil;
            }else{
                self.tableView.mj_footer = self.footer;
                self.page ++;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_footer endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
